/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.academik.validation;

import java.util.Iterator;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.academik.controller.InscripcionBean;
import org.academik.controller.MatriculaBean;
import org.academik.model.Matricula;
import org.academik.model.Persona;
import org.academik.model.Seccion;
import org.academik.util.FacesUtil;
import org.academik.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.StatelessSession;

/**
 *
 * @author Javier
 */
@FacesValidator("inscripcionAlumnoDuplicadoValidator")
public class InscripcionAlumnoDuplicadoValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        boolean exist = false;
        String message = null;
        Long idSeccion = null;
        // try {
        if (component.getAttributes().size() > 0 && component.getAttributes().get("idSeccion") != null) {
            idSeccion = new Long(Long.parseLong(component.getAttributes().get("idSeccion").toString()));
        }


        InscripcionBean m = (InscripcionBean) FacesUtil.getBean("inscripcionBean");
        Session session = HibernateUtil.getSessionFactory().openSession();
        String sql = "SELECT p, a, m "
                + "FROM Matricula m "
                + "JOIN m.alumno a "
                + "JOIN m.seccion s "
                + "JOIN s.anioescolar anio "
                + "JOIN a.persona p "
                + "WHERE p.idPersona = :idPersona AND anio.boEstado = TRUE";
        Query query = session.createQuery(sql);
        query.setParameter("idPersona", m.getPersonaAlumno().getIdPersona());
        query.setMaxResults(1);
        if (query.uniqueResult() != null) {
            exist = true;
            message = "El alumno ya esta matriculado";
            System.out.println(message);

        } else if(!m.isBoEditarMatricula()){
            Iterator iterMatricula = m.getArregloMatriculas().iterator();
            Persona p1 = new Persona();
            while (iterMatricula.hasNext()) {
                Object[] obj = (Object[]) iterMatricula.next();
                p1 = (Persona) obj[1];
                if (m.getPersonaAlumno().getIdPersona().equals(p1.getIdPersona())) {
                    exist = true;
                    message = "El alumno ya esta en la lista";
                    break;
                }
            }
        }
        session.close();
        if (exist) {
            FacesMessage msg = new FacesMessage("Error:", message);
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }
        /*} catch (Exception ex) {
        ex.printStackTrace();
        System.out.print(ex);
        
        
        } */
    }
}
